'''
Write a file, each of whose lines is

src dst res

of interesting nodes. "Interesting" means that both nodes are visible in the
first snapshot, and at least one is visible in the second.
'''

import Parser, re
from copy import deepcopy
from datetime import datetime
from random import random

SkipProbability = 0.99999 # in [0.0, 1.0)         # Change this to the fraction of edges to skip.

#outgoingFromDst = re.compile(r'^')

def nodeInCurrentSnapshot(node):
  try:
    if Parser.seekNodeLite(str(node)):
      return True
  except:
    pass
  return False

if __name__ == '__main__':
  print datetime.now()
  #ss1 = '..\data\graph-2011-08-03.txt'
  #ss2 = '..\data\graph-2011-09-02.txt'
  ss1 = '..\data\graph-2011-09-02.txt'            # Change this to the first snapshot.
  ss2 = '..\data\graph-2011-10-02_old-format.txt' # Change this to the second snapshot.
  outFilename = 'arghsecond_99999.txt'            # Change this to the output file.
  Parser.ParserSelectFile(ss2)
  Parser.initMetaFile()
  Parser.ParserSelectFile(ss1)
  Parser.initMetaFile()
  
  sfile = open(ss1+'.single', 'r')
  sdict = {}
  count = 0
  for line in sfile:
    if count%100000==0:
      print count, datetime.now()
      print len(sdict)
    count+=1
    if not line:
      break
    line = line.strip().split(':')
    try:
      src = int(line[0])
    except:
      continue
    dsts = set()
    if line[1]:
      for dst in line[1].split(','):
        if random() < SkipProbability:
          continue
        try:
          dsts.add(int(dst))
        except:
          continue
    # Ignore destinations that are invisible.
    # Assume that every source node in single is visible!
    if dsts:
      sdict[src] = dsts
  sfile.close()
  
  # sdict should now be a dict whose values are visible dsts of src.
  print datetime.now()
  
  Parser.ParserSelectFile(ss2)
  Parser.initMetaFile()
  
  #output = open('first_999.txt', 'w')
  output = open(outFilename, 'w')
  count = 0
  print len(sdict)
  for src in sdict:
    if count%25==0:
      print count, datetime.now()
    count+=1
    print datetime.now(), 'getting slines'
    slines = Parser.seekNodeLite(str(src))
    if not slines:
      continue
    if len(slines) < 2:
      sfound = False
    elif len(slines) == 2:
      sfound = True
      sline = None
      for l in slines:
        if l.startswith(str(src)+'   1'):
          sline = l.split('   ')
      if not sline:
        continue
    else:
      print 'WARNING: Found multiple node definitions:', slines
      continue
    if sfound:
      print datetime.now(), 'src found, iterating over dsts'
      for dst in sdict[src]:
        if str(dst) in sline[3:-1]:
          output.write(str(src)+' '+str(dst)+' '+'1\n')
        else:
          output.write(str(src)+' '+str(dst)+' '+'0\n')
    else:
      dset = set() # Visible nodes that have an edge to src
      print datetime.now(), 'iterating over slines'
      slines = Parser.seekNode(str(src))
      for l in slines:
        l = l.split('   ')
        #print l
        if l[1]=='0':
          dset.add(int(l[0]))
      print datetime.now(), 'src NOT found, iterating over dsts'
      for dst in sdict[src]:
        if dst in dset:
          # dst is one of the nodes that has an edge to src
          output.write(str(src)+' '+str(dst)+' '+'1\n')
        else:
          # Either invisible or not reciprocated
          print datetime.now(), 'getting dlines'
          dlines = Parser.seekNodeLite(str(dst))
          if not dlines:
            continue # Invisible
          print datetime.now(), 'iterating over dlines'
          for l in dlines:
            if l.startswith(str(dst)+'   0'):
              # It's visible, so it's not reciprocated
              output.write(str(src)+' '+str(dst)+' '+'0\n')
              break
          # Otherwise we simply didn't find it, so it's invisible and do nothing.
  output.close()
  print datetime.now()